package com.bestcem.xm.common.core.uitls;

import com.bestcem.xm.common.core.enums.CommonEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 枚举工具类
 * @date 2021-03-10
 */
public class EnumUtil {

    private EnumUtil() {

    }

    /**
     * 返回指定index的'枚举'
     *
     * @param clazz
     * @param index
     * @param <T>
     * @return
     */
    public static <T extends CommonEnum> T getEnumByIndex(Class<T> clazz, Integer index) {
        if (clazz == null || index == null) {
            return null;
        }

        for (T _enum : clazz.getEnumConstants()) {
            if (index == _enum.getIndex()) {
                return _enum;
            }
        }
        return null;
    }

    /**
     * 返回指定name的'枚举'
     *
     * @param clazz
     * @param name
     * @param <T>
     * @return
     */
    public static <T extends CommonEnum> T getEnumByName(Class<T> clazz, String name) {
        if (clazz == null) {
            return null;
        }

        for (T _enum : clazz.getEnumConstants()) {
            if (StringUtils.equals(name, _enum.getName())) {
                return _enum;
            }
        }
        return null;
    }

    /**
     *  0 否 其余 是
     *
     * @param num
     * @return
     */
    public static boolean intToBoolean(Integer num) {
        return num != null && num != 0;
    }

    public static int booleanToInt(Boolean flag) {
        return (flag != null && flag) ? 1 : 0;
    }
}
